using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using NHibernateDemo.Core;
using NHibernateDemo.DAL;

namespace NHibernateDemo.DAL
{
    public interface INinjaRepository : IRepository<Ninja, int>
    {
        IList<Ninja> GetPage(int pageNumber, int pageSize);
        Ninja GetByName(string name);
    }

    public class NinjaRepository : Repository<Ninja, int>, INinjaRepository
    {
        public Ninja GetByName(string name)
        {                                    
            string HQL = "FROM Ninja ninja INNER JOIN fetch ninja.Weapons WHERE ninja.Name = :Name";
            IQuery query = CreateQuery(HQL).SetParameter("Name", name)
                .SetCacheable(true).SetCacheRegion("NinjaRegion");
            return query.UniqueResult<Ninja>();                     
        }

        public IList<Ninja> GetPage(int pageNumber, int pageSize)
        {
            string HQL = "FROM Ninja";

            IQuery query = CreateQuery(HQL)
                .SetFirstResult(pageNumber)
                .SetMaxResults(pageSize);

            return query.List<Ninja>();
        }
    }

    
}
